layui.use(['form', 'jquery', 'layer'], function () {
    var layer = layui.layer;
    $("#login").click(function () {
        let username = $("#username").val();
        let password = $("#password").val();
        let code = $("#code").val();
        let data = {"code": username, "password": password};
        if (username != "" && password != "") {
            if (code != "") {
                let res = myAjax("/security/security/validateCode", "post", {"code": code});
                if (res.code == 200) {
                    let result = ajaxModel("/security/security/login", "post", data);
                    console.log(result);
                    if (result.code == 200) {
                        layer.alert(result.message, {
                            icon: 1
                        }, function () {
                            sessionStorage.setItem("userInfo", JSON.stringify(result.data.userInfo));
                            sessionStorage.setItem("user_code", result.data.userInfo.code);
                            $.cookie("token", result.data.tokenHead + " " + result.data.access_token);
                            window.location.href = "index.html";
                            layer.close();
                        });
                    } else {
                        layer.msg(result.message);
                        flushCode();
                    }
                }else {
                    layer.msg(res.message);
                    flushCode();
                    $("#codeimg").focus();
                }
            } else {
                layer.msg("验证码不能为空");
                $("#codeimg").focus();
            }
        } else {
            layer.msg("用户名和密码不能为空");
            flushCode();
            $("#username").focus();
        }
    })
    $("#register").click(function () {
        window.location.href = "register.html";
    })
});

//刷新验证码
function flushCode() {
    $("#codeimg").attr("src", "http://localhost:8080/security/security/code?date=" + new Date());
}

function ajaxModel(url, type, data) {
    console.log(data);
    let result = null;
    $.ajax({
        url: "http://localhost:8080" + url,
        type: type,
        async: false,//必须写该句，才能同步处理
        headers: {'Authorization': $.cookie("token")},
        data: JSON.stringify(data),
        dataType: "json",
        contentType: "application/json",
        xhrFields: {
            withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
        },
        success: function (data) {
            result = data;
        },
        error: function (data) {
            result = {code: 500, message: "内部服务器遇到错误"};
            console.log(data);
        }
    });
    return result;
}
function myAjax(url, type, data) {
    console.log(data);
    let user = $.cookie("token");
    let result = null;
    $.ajax({
        url: "http://localhost:8080" + url,
        type: type,
        async: false,//必须写该句，才能同步处理
        headers: {
            "Authorization": $.cookie("token")
        },
        data: data,
        dataType: "json",
        // contentType: "application/json",
        xhrFields: {
            withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
        },
        success: function (data) {
            result = data;
            console.log(result);
            if (result.code==403)
                layer.alert(result.message,{icon:2});
        },
        error: function (data) {
            result = {code: 500, message: "内部服务器遇到错误"};
        }
    });
    return result;
}